package org.erepublik.company.service;

import java.util.HashMap;

import org.erepublik.company.ECountry;
import org.erepublik.company.EProductType;
import org.erepublik.company.PriceIndex;
import org.erepublik.company.charts.AbstractJSONDataCollector;
import org.erepublik.company.charts.CountryJSONDataCollector;
import org.erepublik.company.charts.FoodJSONDataCollector;
import org.erepublik.company.charts.HouseJSONDataCollector;
import org.erepublik.company.charts.TicketJSONDataCollector;
import org.erepublik.company.charts.WeaponJSONDataCollector;

public class PriceIndexService {

    private XmlVatService taxService = new XmlVatService();
    private JSONCountryDataProvider countryDataProvider = null;
    
    public PriceIndexService(JSONCountryDataProvider countryDataProvider) {
        this.countryDataProvider = countryDataProvider;
        dataCollector.put("food", new FoodJSONDataCollector(taxService));
        dataCollector.put("ticket", new TicketJSONDataCollector(taxService));
        dataCollector.put("house", new HouseJSONDataCollector(taxService));
        dataCollector.put("tank", new WeaponJSONDataCollector("tank",taxService));
        dataCollector.put("rifle", new WeaponJSONDataCollector("rifle",taxService));        
        dataCollector.put("artillery", new WeaponJSONDataCollector("artillery",taxService));
        dataCollector.put("helicopter", new WeaponJSONDataCollector("helicopter",taxService));        
        dataCollector.put("currency", new CountryJSONDataCollector(taxService) );      
    }
    
    @SuppressWarnings("unchecked")
    private HashMap<String, AbstractJSONDataCollector> dataCollector = 
            new HashMap<String, AbstractJSONDataCollector>();
    
    
    @SuppressWarnings("unchecked")
    public PriceIndex[] getPricesIndex(String productType, Enum criteria) {
        PriceIndex[] prices = dataCollector.get(productType).getPricesIndex(criteria, EProductType.getProductTypeBySimpleName(productType));
        for (PriceIndex priceIndex : prices) {
            if (priceIndex.getLocation() instanceof ECountry )
                priceIndex.setPopulation( countryDataProvider.getCountryData(((ECountry)priceIndex.getLocation())).getPopulation() );

        }
        return prices;
    }
    
    @SuppressWarnings("unchecked")
    public AbstractJSONDataCollector getDataCollctor(String productName) {
        return dataCollector.get(productName);
    }
    
}
